package ext.tianma.part.finishedProduct.datautilities;

import java.util.ArrayList;

import org.apache.log4j.Logger;

import wt.log4j.LogR;
import wt.part.WTPart;
import wt.util.WTException;

import com.ptc.core.components.descriptor.ModelContext;
import com.ptc.core.components.factory.dataUtilities.DefaultDataUtility;
import com.ptc.core.components.rendering.guicomponents.ComboBox;
import com.ptc.core.components.rendering.guicomponents.StringInputComponent;

import ext.tianma.part.finishedProduct.util.TypesUtil;
import ext.tianma.part.finishedProduct.vo.AttrRuleVo;
import ext.tianma.part.util.IbaAttributeUtil;


public class PartIBAsDataUtility extends DefaultDataUtility{
	private static Logger log = LogR.getLogger(PartIBAsDataUtility.class.getName());
	@Override
	public Object getDataValue(String paramString, Object obj, ModelContext modelContext) throws WTException {
		//当前任务状态
		//获取组件的信息,如下拉列表,约束
		AttrRuleVo attrRuleVo = TypesUtil.queryTypeDef("cn.tianma.FinishedGood", paramString);
		log.debug("PartIBAsDataUtility::partNumber:"+((WTPart) obj).getNumber()+";;paramString:"+paramString+";;attrRuleVo:"+attrRuleVo);

		Object component = super.getDataValue(paramString, obj, modelContext);
		String ibaName = TypesUtil.getAttrIBAInternalName("cn.tianma.FinishedGood", paramString);
		String attributeValue = IbaAttributeUtil.getAttributeValue(obj, ibaName);
		if(String.class.getName().equals(attrRuleVo.getDataType()) 
				&& (attrRuleVo.getEnumMap().size() > 0 
				    || attrRuleVo.getLegalValueList().size() > 0
				    || attrRuleVo.getSuggestedValuesList().size() > 0)){
			ArrayList<String> valueList = new ArrayList<String>();
			ArrayList<String> internaValueList = new ArrayList<String>();
			if(attrRuleVo.getEnumMap().size() > 0){
				valueList = new ArrayList<String>(attrRuleVo.getEnumMap().values());
				valueList.add("");
				internaValueList = new ArrayList<String>(attrRuleVo.getEnumMap().keySet());
				internaValueList.add("");
			}else if(attrRuleVo.getLegalValueList().size() > 0){
				valueList = (ArrayList<String>) attrRuleVo.getLegalValueList();
				valueList.add("");
				internaValueList = (ArrayList<String>) attrRuleVo.getLegalValueList();
				//internaValueList.add("");
			}
			ComboBox combobox = new ComboBox(internaValueList, valueList, new ArrayList());
		    combobox.setId(paramString);
		    combobox.setColumnName(paramString);
		    combobox.setLabel(attrRuleVo.getDisplayName());
		    combobox.setRequired(attrRuleVo.getRequired());
		    combobox.setSelected(attributeValue);
			component = combobox;
		}else if(String.class.getName().equals(attrRuleVo.getDataType())){
			StringInputComponent stringInput = new StringInputComponent();
			stringInput.setId(paramString);
			stringInput.setColumnName(paramString);
			stringInput.setLabel(attrRuleVo.getDisplayName());
			stringInput.setRequired(attrRuleVo.getRequired());
			stringInput.setMaxByteLength(attrRuleVo.getLength());
			stringInput.setValue(attributeValue);
			component = stringInput;
		}
		
	   return component;
	}
	
}
